package com.gd.data.platform.controller.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gd.data.platform.dto.ApiResponse;
import com.gd.data.platform.dto.RealtimeMonitorData;
import com.gd.data.platform.service.realtime.RealtimeDataPushService;
import com.gd.data.platform.service.realtime.RealtimeMonitorService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 实时监控API控制器
 * 提供实时监控数据查询接口
 * 
 * @author gd-data-platform
 * @since 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/realtime")
@Api(tags = "实时监控API")
public class RealtimeMonitorController {

    @Autowired
    private RealtimeMonitorService realtimeMonitorService;

    @Autowired
    private RealtimeDataPushService realtimeDataPushService;

    /**
     * 获取实时监控数据
     */
    @GetMapping("/monitor")
    @ApiOperation("获取实时监控数据")
    public ApiResponse<RealtimeMonitorData> getRealtimeMonitorData() {
        try {
            RealtimeMonitorData data = realtimeMonitorService.getRealtimeMonitorData();
            return ApiResponse.success(data);
        } catch (Exception e) {
            log.error("获取实时监控数据失败", e);
            return ApiResponse.error("获取实时监控数据失败：" + e.getMessage());
        }
    }

    /**
     * 手动推送实时数据
     */
    @PostMapping("/push")
    @ApiOperation("手动推送实时数据")
    public ApiResponse<String> pushRealtimeData() {
        try {
            realtimeDataPushService.pushRealtimeDataManually();
            return ApiResponse.success("实时数据推送成功");
        } catch (Exception e) {
            log.error("推送实时数据失败", e);
            return ApiResponse.error("推送实时数据失败：" + e.getMessage());
        }
    }

    /**
     * 获取WebSocket连接数
     */
    @GetMapping("/connections")
    @ApiOperation("获取WebSocket连接数")
    public ApiResponse<Integer> getConnectionCount() {
        try {
            // 这里需要从WebSocket处理器获取连接数
            // 暂时返回固定值
            return ApiResponse.success(0);
        } catch (Exception e) {
            log.error("获取连接数失败", e);
            return ApiResponse.error("获取连接数失败：" + e.getMessage());
        }
    }
}
